<?php
namespace Macaca\Controller\Admin;

use Macaca;

class GoodsCategory extends Base {

    public function moduleGC() {
        return new Macaca\Module\Admin\GoodsCategory($this->app);
    }

    public function actionGetList() {
        $app = $this->app;
        $view = new Macaca\View\Admin\GoodsCategory($app);
        $module = $this->moduleGC();
        $linker = $this->linker;

        $data['title'] = 'Macaca 商品种类管理';
        $data['url_add'] = $linker->toFix('/admin/goods/category/add');
        $data['url_edit'] = $linker->to('/admin/goods/category/edit');
        $data['url_delete'] = $linker->to('/admin/goods/category/delete');
        $data['url_browse'] = $linker->to('/admin/goods/category/browse');

        $data['gc'] = $module->getSubCategory(0);
        $this->response->setBody($view->render($data));
    }

    public function actionAddItem() {
        $app = $this->app;
        $view = new Macaca\View\Admin\GoodsCategoryEditor($app);
        $module = $this->moduleGC();

        $data['title'] = '添加商品种类';
        $data['url_save'] = $this->linker->toFix('/admin/goods/category/save');

        $data['gc'] = $n = $module->getEmptyItem();
        $data['gc_parent_list'] = $module->getParentList($n['gc_id'], $n['parent_id']);

        $this->response->setBody($view->render($data));
    }

    public function actionGetItem() {
        $app = $this->app;
        $view = new Macaca\View\Admin\GoodsCategoryEditor($app);
        $module = $this->moduleGC();

        $data['title'] = '编辑商品种类';
        $data['url_save'] = $this->linker->toFix('/admin/goods/category/save');

        $gcId = $app->router()->getParameter('gc_id');
        $data['gc'] = $n = $module->getItemById($gcId);
        $data['gc_parent_list'] = $module->getParentList($n['gc_id'], $n['parent_id']);

        if (empty($data['gc'])) {
            $this->writeLine('<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;你要编辑的商品种类不存在!');
        }
        else {
            $this->response->setBody($view->render($data));
        }
    }

    public function actionDeleteItem() {
        $module = $this->moduleGC();
        $nid = $this->app->router()->getParameter('gc_id');

        if ($module->deleteItemById($nid)) {
            $this->writeLine('<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;成功删除所选商品种类!');
        }
        else {
            $this->writeLine('<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;商品种类删除失败!');
        }
    }

    public function actionBrowseItem() {
        $this->writeLine('<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;该功能未实现!');

    }

    public function actionSaveItem() {
        $module = $this->moduleGC();
        if ($module->saveItem($_POST)) {
            $this->writeLine('<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;商品种类保存成功!');
        }
        else {
            $this->writeLine('<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp;商品种类保存失败!');
        }

    }
}